home *** CD-ROM | disk | FTP | other *** search
- Path: lyra.csx.cam.ac.uk!nmm1
- From: nmm1@cus.cam.ac.uk (Nick Maclaren)
- Newsgroups: comp.std.c
- Subject: Re: atexit() and return from main()
- Date: 18 Jan 1996 08:15:48 GMT
- Organization: University of Cambridge, England
- Message-ID: <4dkvjk$6q5@lyra.csx.cam.ac.uk>
- References: <4dhfja$j50@fg70.rz.uni-karlsruhe.de> <4diicg$i9e@lyra.csx.cam.ac.uk> <821923074snz@genesis.demon.co.uk>
- NNTP-Posting-Host: bootes.cus.cam.ac.uk
-
- In article <821923074snz@genesis.demon.co.uk>,
- Lawrence Kirby <fred@genesis.demon.co.uk> wrote:
- >In article <4diicg$i9e@lyra.csx.cam.ac.uk>
- > nmm1@cus.cam.ac.uk "Nick Maclaren" writes:
- >
- >>In article <4dhfja$j50@fg70.rz.uni-karlsruhe.de>, ig25@fg70.rz.uni-karlsruhe.de
- >> (Thomas Koenig) writes:
- >>|> I don't see a problem when I replace the 'return 0' above with
- >>|> an 'exit(0)'.
- >>
- >>I do. exit() may unwind the stack before calling the atexit()
- >>functions, and that is actually the way that most C run-time
- >>systems implement close down.
- >
- >Implementations that do that are non-conforming. According to 7.10.4.3:
- >
- >"First, all functions registered by the atexit function are called, in
- > the reverse order of registration."
- >
- >There is no license given to destroy any existing objects before that happens.
-
- Hang on. The question was whether there was likely to be a problem.
- There is. The fact that such implementations are non-conformant is
- not a denial of that fact.
-
- I can remember discussing this very point at great length with other
- people, and we came to the conclusion that the standard was confusing
- at best. The clarification mentioned by Clive Feather makes it much
- clearer that exit() is NOT supposed to unwind the stack before calling
- the atexit() functions.
-
- But you are making a serious mistake in assuming that run-time systems
- can be implemented to allow everything that the standard permits by
- omission. They can't. In THIS case, they can usually preserve the
- stack before calling the atexit() functions.
-
-
- However, for the advanced paper, think about the following problems:
-
- What is the effect of an atexit() function calling exit()? Does it
- restart the atexit() queue, carry on as if the function had returned
- or does it abandon the atexit() queue?
-
- I chose to do the second, as by far the most user-friendly, but I
- would NEVER write serious code that relied upon that action!
-
- main() calls setjmp() and then calls exit(). An atexit() function
- calls longjmp() and returns to main(). This repeats (or does it?)
-
- This is ALSO permitted by omission, but a good run-time system will
- trap it, issue an error message and stop without calling more atexit()
- functions.
-
-
- Nick Maclaren,
- University of Cambridge Computer Laboratory,
- New Museums Site, Pembroke Street, Cambridge CB2 3QG, England.
- Email: nmm1@cam.ac.uk
- Tel.: +44 1223 334761 Fax: +44 1223 334679
-